/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package Mainframe;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import GuiTools.GuiTools;
import GuiTools.MDManager;
import Inflection.*;
import OtherDialogs.*;
import PictureDescription.*;
import PictureMappingInterrogation.PictureMappingInterrogationSettings;
import Settings.*;
import Storage.Storage;
import VocabularyManagement.*;
import WordTable.WordTablePresettings;
import Wordcheck.WordcheckPreSelection;

/**
 * 
 * Creates the MenuBar for MAINFRAME
 * 
 * @author Christoph Schnack
 *
 */
public class TribuitMenu extends JMenuBar{
	Storage MAINVOC;
	Mainframe MAINFRAME;
	public InfoFrame INFOFRAME;
	public Manual MANUAL;
	public ConjugateDialog CONJUGATEDIALOG;
	OptionDialog OPTIONDIALOG;
	public UTF8Table CHARTABLE;
	public boolean WORDCHECKRUNNING;
	public boolean WORDTABLERUNNING;
	public boolean PICTUREINTERROGATIONRUNNING;
	public MDManager VMDM; //VocabularyManageDialogManager
	public MDManager PMDM; //PictureManageDialogManager
	public MDManager PIDM; //PictureInterrogationDialogManager
	PictureStorage PICSTO;
	Language LANG;
	Settings SET;
	JMenu tribuit, latin, info, vocabulary, pictures ;
	JMenuItem options,about,manage,quit,wordCheck,conjugate,declinate,wordtable,manual,charTable,pictureManager,pictureInterrogation;
  	TribuitMenu MENU;
	public TribuitMenu(Storage stor,PictureStorage picsto, Mainframe mframe,Settings st){
		MENU=this;
		MAINVOC = stor;
		MAINFRAME = mframe;
		LANG = st.getLanguage();
		SET = st;
		PICSTO=picsto;
		
    	tribuit = new JMenu(LANG.getMenuProgram());
    	vocabulary = new JMenu(LANG.getVocabulary());
    	pictures = new JMenu(LANG.getPictures());
    	latin = new JMenu(LANG.getMenuLatin());
      	info = new JMenu(LANG.getMenuInfo());

      	quit = new JMenuItem(LANG.getQuit());
      	pictureManager = new JMenuItem(LANG.getManager());
      	pictureInterrogation = new JMenuItem(LANG.getInterrogation());
      	wordCheck = new JMenuItem(LANG.getMenuWordcheck());
      	wordCheck.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,Event.ALT_MASK));
      	wordtable = new JMenuItem(LANG.getMenuWordTable());
      	conjugate = new JMenuItem(LANG.getMenuConjugate());
      	declinate = new JMenuItem(LANG.getMenuDeclinate());
      	charTable = new JMenuItem(LANG.getSpecialCharacter());
      	manual = new JMenuItem(LANG.getManual());
      	declinate.setEnabled(false);
      	manage = new JMenuItem(LANG.getMenuManage());
      	manage.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.ALT_MASK));
      	about = new JMenuItem(LANG.getAbout());
	  	options = new JMenuItem(LANG.getSettings());

	  	pictureManager.addActionListener(new PictureManagerListener());
	  	pictureInterrogation.addActionListener(new PictureInterrogationListener());
		wordCheck.addActionListener(new WordCheckListener());
		wordtable.addActionListener(new WordTableListener());
		charTable.addActionListener(new CharTableListener());
		manual.addActionListener(new ManualListener());
      	quit.addActionListener(new QuitListener());
      	manage.addActionListener(new ManageListener());
	  	options.addActionListener(new OptionsListener());
      	about.addActionListener(new InfoListener());
      	conjugate.addActionListener(new ConjugateListener());
      	declinate.addActionListener(new DeclinateListener());

      	latin.add(conjugate);
      	//latin.add(declinate);
      	vocabulary.add(manage);
		vocabulary.add(wordCheck);
		vocabulary.add(wordtable);
		pictures.add(pictureManager);
		pictures.add(pictureInterrogation);
		tribuit.add(vocabulary);
		tribuit.add(pictures);
		tribuit.addSeparator();
        tribuit.add(latin);
        tribuit.addSeparator();
        tribuit.add(charTable);
        tribuit.add(options);
        tribuit.addSeparator();
        tribuit.add(quit);
	    info.add(about);
	    info.add(manual);
	    this.add(tribuit);
		add(info);
	}
	
	public MDManager getVMDM(){
		return VMDM;
	}
	
	public void updateLanguage(){
		LANG = SET.getLanguage();
		quit.setText(LANG.getQuit());
      	wordCheck.setText(LANG.getMenuWordcheck());
      	manage.setText(LANG.getMenuManage());
      	about.setText(LANG.getAbout());
	  	options.setText(LANG.getSettings());
	  	manual.setText(LANG.getManual());
	  	wordtable.setText(LANG.getMenuWordTable());
	  	latin.setText(LANG.getMenuLatin());
	  	conjugate.setText(LANG.getMenuConjugate());
	  	options.setText(LANG.getSettings());
	  	charTable.setText(LANG.getSpecialCharacter());
	  	info.setText(LANG.getMenuInfo());
	  	pictures.setText(LANG.getPictures());
	  	vocabulary.setText(LANG.getVocabulary());
	  	pictureManager.setText(LANG.getManager());
	  	pictureInterrogation.setText(LANG.getInterrogation());
	}
	
	public void setWordcheckRunning(boolean bool){WORDCHECKRUNNING=bool;}
	public boolean getWordcheckRunning(){return WORDCHECKRUNNING;}
   
	class WordCheckListener implements ActionListener{
 		public void actionPerformed(ActionEvent e){
 			if(!WORDCHECKRUNNING){
 				new WordcheckPreSelection(MAINVOC,SET,MAINFRAME,MENU);
 				WORDCHECKRUNNING=true;
 			}
     	}
 	}
	
	class DeclinateListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			new Inflection("declinate",MAINVOC,SET);
		}
	}
	class ConjugateListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(CONJUGATEDIALOG==null || !CONJUGATEDIALOG.isDisplayable()){
				CONJUGATEDIALOG = new ConjugateDialog(MAINVOC,SET);
			}
			else{
				if(CONJUGATEDIALOG.isDead()){
					CONJUGATEDIALOG = new ConjugateDialog(MAINVOC,SET);
				}
				else{
					Point pos = MAINFRAME.getLocationOnScreen();
					if(CONJUGATEDIALOG.getState()==JFrame.ICONIFIED) CONJUGATEDIALOG.setState(JFrame.NORMAL);
					CONJUGATEDIALOG.setLocation(pos.x+300,pos.y);
					CONJUGATEDIALOG.requestFocus();
				}
			}
		}
	}
	class ManualListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(MANUAL==null || !MANUAL.manualFrame.isDisplayable())MANUAL = new Manual(MAINFRAME,MAINVOC,SET);
			else{
				if(MANUAL.manualFrame.getState()==JFrame.ICONIFIED) MANUAL.manualFrame.setState(JFrame.NORMAL);
				MANUAL.manualFrame.showUpAt(MAINFRAME);
			}
		}
	}
	
	public void setWordtableRunning(boolean bool){WORDTABLERUNNING=bool;}
	public boolean getWordtableRunning(){return WORDTABLERUNNING;}
	
	class WordTableListener implements ActionListener{
  		public void actionPerformed(ActionEvent e){
  			if(!WORDTABLERUNNING){
  				WORDTABLERUNNING=true;
  				new WordTablePresettings(MAINFRAME,MAINVOC,SET,MENU);
  			}
  		}
	}
	class CharTableListener implements ActionListener{
  		public void actionPerformed(ActionEvent e){
  			if(CHARTABLE==null || !CHARTABLE.isDisplayable()){
				CHARTABLE = new UTF8Table(SET);
			}
			else{
				if(CHARTABLE.getState()==JFrame.ICONIFIED) CHARTABLE.setState(JFrame.NORMAL);
				CHARTABLE.showUpAt(MAINFRAME);
			}
  		}
	}
	
	class OptionsListener implements ActionListener{
    	public void actionPerformed(ActionEvent e){
			if(OPTIONDIALOG==null || !OPTIONDIALOG.isDisplayable()){
				OPTIONDIALOG = new OptionDialog(MAINVOC,SET,MAINFRAME,MENU);
			}
			else{
				if(OPTIONDIALOG.getState()==JFrame.ICONIFIED) OPTIONDIALOG.setState(JFrame.NORMAL);
				OPTIONDIALOG.showUpAt(MAINFRAME);
			}
	    }
	}

	class QuitListener implements ActionListener{
    	public void actionPerformed(ActionEvent e){
			GuiTools.exitTribuit(MAINVOC,MAINFRAME,SET);
	    }
	}
		


	class ManageListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
			if(VMDM==null || !VMDM.isRunning()){
				if(VMDM==null)VMDM=new MDManager(MAINFRAME);
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
			else{
				VMDM.focusCurrentDialog();
			}
	    }
	}
	
	class InfoListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
			if(INFOFRAME==null || !INFOFRAME.isDisplayable())INFOFRAME = new InfoFrame(SET, MAINFRAME);
			else{
				if(INFOFRAME.getState()==JFrame.ICONIFIED) INFOFRAME.setState(JFrame.NORMAL);
				INFOFRAME.showUpAt(MAINFRAME);
			}
	    }
	}
	
	class PictureManagerListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	    	if(PMDM==null || !PMDM.isRunning()){
				PMDM=new MDManager(MAINFRAME);
				new PictureManageDialog(PICSTO,SET,MAINFRAME,PMDM);
			}
			else{
				PMDM.focusCurrentDialog();
			}
	    }
	}
	
	class PictureInterrogationListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	    	if(PIDM==null || !PIDM.isRunning()){
	    		PIDM = new MDManager(MAINFRAME);
	    		new PictureMappingInterrogationSettings(MAINFRAME,SET,PICSTO,PIDM);
	    	}
	    	else{
				PIDM.focusCurrentDialog();
			}
	    }
	}
	
	public void fireWordTable(){
		wordtable.doClick();
	}
	
	public void fireVocabularyManager(){
		manage.doClick();
	}
	public void fireCharTable(){
		charTable.doClick();
	}
	public void fireWordcheck(){
		wordCheck.doClick();
	}
}//menu